package com.fortune.service.impl;

import com.fortune.constants.Constants;
import com.fortune.dao.ProductCategoryDao;
import com.fortune.dto.in.productCategory.PProduceCategoryAddDto;
import com.fortune.dto.in.productCategory.PProduceCategoryEditDto;
import com.fortune.entity.ProductCategoryDo;
import com.fortune.service.ProductCategoryService;
import com.fortune.util.UUIDUtil;
import com.fortune.vo.productCategory.ProductCategory;
import com.fortune.vo.productCategoryMgt.ProductCategoryVo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class ProductCategoryServiceImpl implements ProductCategoryService {
    @Autowired
    private ProductCategoryDao productCategoryDao;

    /**
     * 查询商品分类
     * @return
     */
    @Override
    public List<ProductCategory> queryProductCategory() {
        return productCategoryDao.queryProductCategory();
    }

	@Override
	public List<ProductCategoryVo> getTreeCategories() {
		List<ProductCategoryDo> productCategories = productCategoryDao.getAllProductCategories();
		List<ProductCategoryVo> vos = new ArrayList<>();
		LinkedList<ProductCategoryDo> linkedList = new LinkedList<>(productCategories);
		generateTree(vos, linkedList, Constants.PRODUCT_ROOT_CATEGORY_PID);
		return vos;
	}

	/**
	 * 查询分类名称是否存在
	 * @param name
	 * @return
	 */
	@Override
	public int queryProductCategoryByName(String name) {
		return productCategoryDao.queryProductCategoryByName(name);
	}

	/**
	 * 添加分类
	 * @param pProduceCategoryAddDto
	 * @return
	 */
	@Override
	public String insertProductCategory(PProduceCategoryAddDto pProduceCategoryAddDto) {
		String uuid = UUIDUtil.get32UUID();
		productCategoryDao.insertProductCategory(pProduceCategoryAddDto,uuid);
		return uuid;
	}

	/**
	 * 查询分类名称是否存在
	 * @param id
	 * @return
	 */
	@Override
	public int getCategoryById(String id) {
		return productCategoryDao.getCategoryById(id);
	}

	@Override
	public int findCategoryByPro(String id){
		return productCategoryDao.findCategoryByPro(id);
	};

	/**
	 * 编辑分类名称
	 * @param pProduceCategoryEditDto
	 * @return
	 */
	@Override
	public int editCategory(PProduceCategoryEditDto pProduceCategoryEditDto) {
		return productCategoryDao.editCategory(pProduceCategoryEditDto);
	}

	/**
	 * 删除分类名称
	 * @param id
	 * @return
	 */
	@Override
	public int deleteCategory(String id) {
		return productCategoryDao.deleteCategory(id);
	}

	private void generateTree(List<ProductCategoryVo> productCategoryVos, LinkedList<ProductCategoryDo> categories, String pid) {
		for(int i=categories.size()-1;i>-1;i--) {
			if (categories.get(i).getPid().equals(pid)) {
				ProductCategoryVo pcVo = new ProductCategoryVo();
				pcVo.setId(categories.get(i).getId());
				pcVo.setPid(categories.get(i).getPid());
				pcVo.setName(categories.get(i).getName());
				productCategoryVos.add(pcVo);
				categories.remove(i);
			}
		}
		Collections.sort(productCategoryVos, new Comparator<ProductCategoryVo>() {
			@Override
			public int compare(ProductCategoryVo o1, ProductCategoryVo o2) {
				return -1;
			}
		});
		for (ProductCategoryVo vo : productCategoryVos) {
			vo.setProductCategories(new ArrayList<ProductCategoryVo>());
			generateTree(vo.getProductCategories(), categories, vo.getId());
		}

	}
}
